import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation
import statistics as st
import yfinance as yf
import tensorflow as tf
import PIL
import math
import warnings
warnings.filterwarnings("ignore")
2023-08-22 12:56:30.853827: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
from IPython.display import HTML
from tensorflow import keras
from tensorflow.keras import layers
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, BatchNormalization, Dropout
from keras.callbacks import EarlyStopping
from tensorflow.keras import regularizers
from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.optimizers import Adam
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
class Stock:
def __init__(self, ticker, period):
self.ticker = ticker
self.period = period
def chart(self):
return yf.download(self.ticker.upper(), period=self.period)
hsbc = Stock("HSBA.L", "5Y").chart()
lloyds = Stock("LLOY.L", "5Y").chart()
[*********************100%***********************] 1 of 1 completed [*********************100%***********************] 1 of 1 completed
class StandardScale:
def __init__ (self, data):
self.data = data
def scale_fit(self):
return (self.data - self.data.mean())/self.data.std()
hsbc['Close'] = hsbc['Close']/100
hsbc[:5]
| Open | High | Low | Close | Adj Close | Volume | |
|---|---|---|---|---|---|---|
| Date | ||||||
| 2018-08-22 | 691.000000 | 694.000000 | 689.099976 | 6.893 | 533.428467 | 28618071 |
| 2018-08-23 | 689.400024 | 690.099976 | 685.099976 | 6.882 | 532.577209 | 20936281 |
| 2018-08-24 | 687.099976 | 688.599976 | 684.299988 | 6.862 | 531.029602 | 17636211 |
| 2018-08-28 | 694.200012 | 695.900024 | 685.900024 | 6.906 | 534.434570 | 21250214 |
| 2018-08-29 | 693.099976 | 694.599976 | 683.400024 | 6.849 | 530.023560 | 21954676 |
hsbc = StandardScale(hsbc).scale_fit()
hsbc[:5]
| Open | High | Low | Close | Adj Close | Volume | |
|---|---|---|---|---|---|---|
| Date | ||||||
| 2018-08-22 | 1.661454 | 1.644679 | 1.689540 | 1.647340 | 0.919598 | -0.116000 |
| 2018-08-23 | 1.646161 | 1.607335 | 1.651309 | 1.636796 | 0.909903 | -0.543748 |
| 2018-08-24 | 1.624175 | 1.592972 | 1.643663 | 1.617627 | 0.892277 | -0.727507 |
| 2018-08-28 | 1.692043 | 1.662873 | 1.658956 | 1.659800 | 0.931057 | -0.526267 |
| 2018-08-29 | 1.681528 | 1.650424 | 1.635061 | 1.605167 | 0.880818 | -0.487040 |
lloyds['Close'] = lloyds['Close']/100
lloyds[:5]
| Open | High | Low | Close | Adj Close | Volume | |
|---|---|---|---|---|---|---|
| Date | ||||||
| 2018-08-22 | 60.540001 | 61.320000 | 60.540001 | 0.6100 | 46.861271 | 175839001 |
| 2018-08-23 | 60.950001 | 60.980000 | 60.380001 | 0.6076 | 46.676895 | 230949635 |
| 2018-08-24 | 60.580002 | 61.220001 | 60.360001 | 0.6084 | 46.738354 | 145231507 |
| 2018-08-28 | 61.259998 | 61.639999 | 60.630001 | 0.6079 | 46.699947 | 128154783 |
| 2018-08-29 | 60.770000 | 60.919998 | 59.880001 | 0.6046 | 46.446430 | 198641536 |
lloyds = StandardScale(lloyds).scale_fit()
lloyds[:5]
| Open | High | Low | Close | Adj Close | Volume | |
|---|---|---|---|---|---|---|
| Date | ||||||
| 2018-08-22 | 1.395586 | 1.414171 | 1.459984 | 1.448087 | 0.838523 | -0.336483 |
| 2018-08-23 | 1.437804 | 1.379009 | 1.443520 | 1.423309 | 0.813155 | 0.192040 |
| 2018-08-24 | 1.399705 | 1.403830 | 1.441462 | 1.431568 | 0.821611 | -0.630015 |
| 2018-08-28 | 1.469725 | 1.447266 | 1.469245 | 1.426406 | 0.816327 | -0.793785 |
| 2018-08-29 | 1.419269 | 1.372803 | 1.392069 | 1.392336 | 0.781447 | -0.117802 |
X = hsbc.Close.values.reshape(-1,1).astype('float')
y = lloyds.Close.values.reshape(-1,1).astype('float')
X.shape
(1262, 1)
X = X[:1260]
X.shape
(1260, 1)
y.shape
(1260, 1)
X_test, X_train, y_test, y_train = train_test_split(X,y, test_size=0.2)
print(X_test.shape)
print(y_test.shape)
(1008, 1) (1008, 1)
print(X_train.shape)
print(y_train.shape)
(252, 1) (252, 1)
model = LinearRegression().fit(X_train, y_train)
model
LinearRegression()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
LinearRegression()
y_pred = model.predict(X_test)
plt.figure(figsize=(10,5))
plt.scatter(X_train, y_train, s=20)
plt.plot(X_test, y_pred, color="red")
plt.xlabel('LLOYDS BANKS')
plt.ylabel('HSBC HOLDINGS')
plt.title('Correaltion between LLOYDS & HSBC')
plt.grid(True, linestyle="--", color="blue", alpha=0.4)
plt.show()
mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
r_square = r2_score(y_test, y_pred)
print("Mean Absolute Error:", mae)
print("Mean Squared Error:", mse)
print("Root Mean Squared Error:", rmse)
print("R-squared:", r_square)
Mean Absolute Error: 0.45636300621391074 Mean Squared Error: 0.31258313014377354 Root Mean Squared Error: 0.55909134329175 R-squared: 0.6796129217150708
A local minimum is a point where our function is lower than all neighboring points. It is not possible to decrease the value of the cost function by making infinitesimal steps.
A global minimum is a point that obtains the absolute lowest value of our function, but global minima are difficult to compute in practice.
class GradientDescent:
def __init__(self, x, y, m_curr=0, c_curr=0, iteration=500, rate=0.01):
self.x = x
self.y = y
self.predicted_y = (m_curr * x) + c_curr # Initialize predicted_y using initial slope and intercept
self.m_curr = m_curr
self.c_curr = c_curr
self.iteration = iteration
self.rate = rate
def cost_function(self):
N = len(self.y)
#mean squared error
return sum((self.y - self.predicted_y) ** 2) / N
def calculation(self):
N = float(len(self.y))
gradient_descent = pd.DataFrame(columns=['m_curr', 'c_curr', 'cost'])
# Perform gradient descent iterations
for i in range(self.iteration):
# Calculate the predicted y values using current slope and intercept
self.predicted_y = (self.m_curr * self.x) + self.c_curr
cost = self.cost_function()
# Calculate gradients for slope (m_grad) and intercept (c_grad)
m_gradient = -(2/N) * np.sum(self.x * (self.y - self.predicted_y))
c_gradient = -(2/N) * np.sum(self.y - self.predicted_y)
# Update the slope and intercept using gradient and learning rate
self.m_curr -= self.rate * m_gradient
self.c_curr -= self.rate * c_gradient
gradient_descent.loc[i] = [self.m_curr, self.c_curr, cost]
return gradient_descent
gd = GradientDescent(X_train, y_train).calculation()
gd['cost'] = gd['cost'].explode()
plt.figure(figsize=(10,5))
gd.cost.plot(color="red", linestyle="--")
plt.scatter(gd.index[-1:], gd.cost[-1:], s=30, color="grey")
plt.scatter(gd.index[0:1], gd.cost[0:1], s=30, color="grey")
plt.ylabel('Cost')
plt.xlabel('Iteration')
plt.title('Gradient descent')
plt.grid(True, linestyle="--", color="blue", alpha=0.4)
plt.show()
fig = plt.figure()
fig, ax = plt.subplots(figsize=(15, 6))
plt.ylabel("LLOYDS", fontsize=16)
plt.xlabel("HSBC", fontsize=16)
plt.title('Linear Regression', fontsize=16)
plt.grid(True, linestyle="--")
plt.scatter(X_train, y_train, color='gray', s=40)
line, = ax.plot([], [], lw=2)
annotation = ax.annotate('', xy=(0.5, 0.95), xycoords='axes fraction', fontsize=10)
def init():
line.set_data([], [])
annotation.set_text('')
return line, annotation
def animate(i):
m_curr, c_curr, cost = gd.loc[i]
x_vals = X_test
y_pred = m_curr * x_vals + c_curr
line.set_data(x_vals, y_pred)
annotation.set_text('Cost = %.2f' % cost)
return line, annotation
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len(gd), interval=40, blit=True)
HTML(anim.to_jshtml())
Animation size has reached 21020100 bytes, exceeding the limit of 20971520.0. If you're sure you want a larger animation embedded, set the animation.embed_limit rc parameter to a larger value (in MB). This and further frames will be dropped.